package cn.tedu.controller.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginIntercptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(
            HttpServletRequest req,
            HttpServletResponse res,
            Object o) throws Exception {
//        System.out.println("LoginIntercptor 拦截器开始执行");
        HttpSession session = req.getSession();
        Object obj = session.getAttribute("uid");
        String url = req.getContextPath() + "/main/index.do";
        if (obj == null){
            System.out.println("没有登录被重定向");
            res.sendRedirect(url);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
